#include  "t_usart_hw.h"
//#include "main.h"

extern void Error_Handler(void);

volatile uint16_t rx4_len ;  //接收一帧数据的长度
volatile uint8_t recv4_end_flag ; //一帧数据接收完成标志
uint8_t rx4_buffer[BUFFER_SIZE];  //接收数据缓存数组


UART_HandleTypeDef slm_huart4;
DMA_HandleTypeDef  slm_hdma_uart4_rx;
DMA_HandleTypeDef  slm_hdma_uart4_tx;

void bsp_uart4_init(void){

  slm_huart4.Instance = UART4;
  slm_huart4.Init.BaudRate = 115200;
  slm_huart4.Init.WordLength = UART_WORDLENGTH_8B;
  slm_huart4.Init.StopBits = UART_STOPBITS_1;
  slm_huart4.Init.Parity = UART_PARITY_NONE;
  slm_huart4.Init.Mode = UART_MODE_TX_RX;
  slm_huart4.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  slm_huart4.Init.OverSampling = UART_OVERSAMPLING_16;
  if (HAL_UART_Init(&slm_huart4) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN UART4_Init 2 */
   __HAL_UART_ENABLE_IT(&slm_huart4, UART_IT_IDLE); //使能IDLE中断

  //DMA接收函数，此句一定要加，不加接收不到第一次传进来的实数据，是空的，且此时接收到的数据长度为缓存器的数据长度
	HAL_UART_Receive_DMA(&slm_huart4,rx4_buffer,BUFFER_SIZE);
  /* USER CODE END UART4_Init 2 */

}

void HAL_UART4_MspInit(UART_HandleTypeDef* uartHandle)
{

  GPIO_InitTypeDef GPIO_InitStruct = {0};
  if(uartHandle->Instance==UART4)
  {
  /* USER CODE BEGIN UART4_MspInit 0 */

  /* USER CODE END UART4_MspInit 0 */
    /* UART4 clock enable */
    __HAL_RCC_UART4_CLK_ENABLE();

    __HAL_RCC_GPIOC_CLK_ENABLE();
    /**UART4 GPIO Configuration
    PC10     ------> UART4_TX
    PC11     ------> UART4_RX
    */
    GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF8_UART4;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);

    /* UART4 DMA Init */
    /* UART4_RX Init */
    slm_hdma_uart4_rx.Instance = DMA1_Stream2;
    slm_hdma_uart4_rx.Init.Channel = DMA_CHANNEL_4;
    slm_hdma_uart4_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
    slm_hdma_uart4_rx.Init.PeriphInc = DMA_PINC_DISABLE;
    slm_hdma_uart4_rx.Init.MemInc = DMA_MINC_ENABLE;
    slm_hdma_uart4_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    slm_hdma_uart4_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    slm_hdma_uart4_rx.Init.Mode = DMA_NORMAL;
    slm_hdma_uart4_rx.Init.Priority = DMA_PRIORITY_LOW;
    slm_hdma_uart4_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&slm_hdma_uart4_rx) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(uartHandle,hdmarx,slm_hdma_uart4_rx);

    /* UART4_TX Init */
    slm_hdma_uart4_tx.Instance = DMA1_Stream4;
    slm_hdma_uart4_tx.Init.Channel = DMA_CHANNEL_4;
    slm_hdma_uart4_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
    slm_hdma_uart4_tx.Init.PeriphInc = DMA_PINC_DISABLE;
    slm_hdma_uart4_tx.Init.MemInc = DMA_MINC_ENABLE;
    slm_hdma_uart4_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
    slm_hdma_uart4_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
    slm_hdma_uart4_tx.Init.Mode = DMA_NORMAL;
    slm_hdma_uart4_tx.Init.Priority = DMA_PRIORITY_LOW;
    slm_hdma_uart4_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
    if (HAL_DMA_Init(&slm_hdma_uart4_tx) != HAL_OK)
    {
      Error_Handler();
    }

    __HAL_LINKDMA(uartHandle,hdmatx,slm_hdma_uart4_tx);

    /* UART4 interrupt Init */
    HAL_NVIC_SetPriority(UART4_IRQn, 7, 0);
    HAL_NVIC_EnableIRQ(UART4_IRQn);
  /* USER CODE BEGIN UART4_MspInit 1 */

  /* USER CODE END UART4_MspInit 1 */
  }
}

void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
{

  if(uartHandle->Instance==UART4)
  {
 
    __HAL_RCC_UART4_CLK_DISABLE();
    HAL_GPIO_DeInit(GPIOC, GPIO_PIN_10|GPIO_PIN_11);
    HAL_DMA_DeInit(uartHandle->hdmarx);
    HAL_DMA_DeInit(uartHandle->hdmatx);
    HAL_NVIC_DisableIRQ(UART4_IRQn);
  }
  
}
void usart_DMA_Init(void)
{ 
	  __HAL_RCC_DMA1_CLK_ENABLE();
  //__HAL_RCC_DMA2_CLK_ENABLE();
  HAL_NVIC_SetPriority(DMA1_Stream4_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream4_IRQn);
  HAL_NVIC_SetPriority(DMA1_Stream2_IRQn, 2, 0);
  HAL_NVIC_EnableIRQ(DMA1_Stream2_IRQn);

}

void DMA1_Stream4_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&slm_hdma_uart4_tx);
}
void DMA1_Stream2_IRQHandler(void)
{
  HAL_DMA_IRQHandler(&slm_hdma_uart4_rx);
}
#include "at_slm332.h"


extern t_at_t *SLM,*ML307R;
void UART4_IRQHandler(void)
{
	HAL_UART_IRQHandler(&slm_huart4);
	if(__HAL_UART_GET_FLAG(&slm_huart4,UART_FLAG_IDLE)!= RESET)//idle标志被置位
	{ 
		__HAL_UART_CLEAR_IDLEFLAG(&slm_huart4);//清除标志位
		HAL_UART_DMAStop(&slm_huart4); //
		rx4_len = BUFFER_SIZE - __HAL_DMA_GET_COUNTER(&slm_hdma_uart4_rx);// 获取DMA中未传输的数据个数
    HAL_UART_Receive_DMA(&slm_huart4, (uint8_t *)rx4_buffer, BUFFER_SIZE);//DMA接收
    //HAL_UART_Transmit_DMA(&slm_huart4, (uint8_t *)rx4_buffer,rx4_len);//打环处理
		if(rx4_len>0) 
		{
		 recv4_end_flag = 1;	// 接受完成标志位置1	
		 //t_at_rev_push_rbf(SLM,rx4_buffer,rx4_len);//MG 处理线程
			t_at_rev_push_data_rbf(ML307R,rx4_buffer,rx4_len,"publish");
			
		}
	 }
}

void t_at_hw_usart_init(void){

	 usart_DMA_Init();
	 bsp_uart4_init();
}